/**
 * Copyright © 2011 looty
 * @link http://www.looty.org
 * @link http://code.google.com/p/looty/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.data 
{

	public class DelayedCallList 
	{
		
		private var _currentTime		:uint;
		
		private var _delayedCalls		:Vector.<DelayedCall>;
		private var _length				:uint;
		private var _inc				:uint;
		
		public function DelayedCallList() 
		{
			_delayedCalls = new Vector.<DelayedCall>();
		}
		
		public function add(delay:Number, callback:Function, params:Array):void
		{
			_delayedCalls[_delayedCalls.length] = new DelayedCall(delay, callback, params);
			++ _length;
		}
		
		public function remove(callback:Function):void
		{
			_inc = 0;
			
			while (_inc < _length)
			{
				if (_delayedCalls[_inc].callback != callback) ++_inc;
				else
				{
					_delayedCalls.splice(_inc, 1)[0].dispose();;
					--_length;
				}
			}			
		}
		
		public function get currentTime():uint 
		{
			return _currentTime;
		}
		
		public function set currentTime(value:uint):void 
		{
			_currentTime = value;
			
			_inc = 0;
			
			while (_inc < _length)
			{
				if (_delayedCalls[_inc].triggerAt <= _currentTime)
				{
					_delayedCalls[_inc].execute();
					_delayedCalls.splice(_inc, 1)[0].dispose();;
					--_length;
				}
				else ++_inc;
			}
		
		}
		
	}

}